package ge.telasi.tasks;

import java.io.IOException;
import javax.persistence.EntityManager;
import javax.persistence.EntityManagerFactory;
import javax.persistence.Persistence;

/**
 * @author dimitri
 */
public final class PersistenceUtils {
    static String SCHEMA_NAME = "tasks";
    static EntityManagerFactory emf;

    public static String getSchema() throws IOException {
        return SCHEMA_NAME;
    }

    public static EntityManagerFactory createEmFactory() throws IOException {
        if (emf != null && emf.isOpen()) {
            return emf;
        }
        emf = Persistence.createEntityManagerFactory("tasks_oracle_pu");
        return emf;
    }

    public static void beginTransaction(EntityManager em) {
        em.getTransaction().begin();
    }

    public static void endTransaction(EntityManager em) {
		if (em.getTransaction().isActive()) {
			if (em.getTransaction().getRollbackOnly()) // rollback
				em.getTransaction().rollback();
            else // commit
    			em.getTransaction().commit();
		}
	}
    
}
